local discard = fk.CreateSkill{
  name = "#rfenghou_3d15j__discard_skill",
}

-- TODO: 目前只有赵娥使用此机制

--- 获取某牌占用的手牌上限
---@param player Player
---@param card Card
---@return integer
local function getMaxCardsOccupied (player, card)
  local maxcard_skills = Fk:currentRoom().status_skills[MaxCardsSkill] or Util.DummyTable
  if table.find(maxcard_skills, function(skill)
    return skill:excludeFrom(player, card)
  end) then return 0 end
  -- 在此处修改卡牌占用手牌上限
  -- TODO: 需要自定义状态技
  if player:getMark("@rfenghou_3d15j__cuixue-turn") ~= 0 and card.trueName == "slash" then
    return tonumber(player:getMark("@rfenghou_3d15j__cuixue-turn")) or 1
  end
  return 1
end

-- 模拟弃牌阶段
---@param player ServerPlayer
local function discardPhase(player)
  local room = player.room
  local limit = player:getMaxCards()
  local occupied = 0
  for _, id in ipairs(player:getCardIds(Player.Hand)) do
    occupied = occupied + getMaxCardsOccupied(player, Fk:getCardById(id))
  end
  local discardNum = occupied - limit
  if discardNum <= 0 then return end
  local canDiscards = table.filter(player:getCardIds(Player.Hand), function(id)
    return getMaxCardsOccupied(player, Fk:getCardById(id)) > 0 and not player:prohibitDiscard(id)
  end)
  if #canDiscards == 0 then return end
  local default = {}
  local tempNum = discardNum
  for _, id in ipairs(canDiscards) do
    table.insert(default, id)
    tempNum = tempNum - getMaxCardsOccupied(player, Fk:getCardById(id))
    if tempNum <= 0 then break end
  end
  if #default == 0 then return end
  if #default < #canDiscards then
    local _ , dat = room:askToUseActiveSkill(player, {
      skill_name = "rfenghou_3d15j__discard_skill",
      prompt = "#rfenghou_3d15j__discard_skill:::"..discardNum,
      cancelable = false,
      extra_data = {skillName = "phase_discard", discardNum = discardNum}
    })
    if dat then
      default = dat.cards
    end
  end
  room:throwCard(default, "phase_discard", player, player)
end

discard:addEffect("active", {
  card_filter = function (self, player, to_select, cards)
    if not table.contains(player.player_cards[Player.Hand], to_select)
    or getMaxCardsOccupied(player, Fk:getCardById(to_select)) <= 0
    or player:prohibitDiscard(to_select) then return false end
    return not self:feasible(player, {}, cards)
  end,
  target_filter = Util.FalseFunc,
  feasible = function (self, player, cards)
    local num = 0
    local discardNum = self.discardNum or 1
    for _, id in ipairs(cards) do
      num = num + getMaxCardsOccupied(player, Fk:getCardById(id))
    end
    return num >= discardNum
  end,
})

discard:addEffect(fk.EventPhaseProceeding, {
  global = true,
  mute = true,
  priority = 0.001,
  can_trigger = function (self, event, target, player, data)
    if target == player and player.phase == Player.Discard and not player:isKongcheng() then
      return player:getMark("@rfenghou_3d15j__cuixue-turn") ~= 0
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    if data.phase_end then return end
    discardPhase(player)
    data.phase_end = true
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d15j__discard_skill"] = "弃牌",
  ["#rfenghou_3d15j__discard_skill"] = "请弃置 %arg 张牌",
  [":rfenghou_discard_skill"] = "每张手牌所占用的手牌上限默认为1，牌不计入手牌上限=牌占用手牌上限为0。" ..
  "<br>弃牌阶段，你改为弃置占用手牌上限数和为X的手牌。（X为你的手牌占用手牌上限和-手牌上限）",
}

return discard
